package com.rocker.graduation.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.rocker.graduation.constants.Constants;
import com.rocker.graduation.dao.CartMapper;
import com.rocker.graduation.dao.CategoryMapper;
import com.rocker.graduation.dao.EntityMapper;
import com.rocker.graduation.dao.InventoryMapper;
import com.rocker.graduation.domain.Cart;
import com.rocker.graduation.domain.CartExample;
import com.rocker.graduation.domain.Inventory;
import com.rocker.graduation.domain.InventoryExample;
import com.rocker.graduation.entity.CartBean;
import com.rocker.graduation.exception.RemoteSensingException;
import com.rocker.graduation.service.ICartService;

@Service
public class CartService implements ICartService {
	@Autowired
	private InventoryMapper inventoryMapper;
	
	@Autowired
	private CategoryMapper categoryMapper;
	
	@Autowired
	private CartMapper cartMapper;
	
	@Autowired
	private EntityMapper entityMapper;
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.ICartService#addToCart(java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public List<CartBean> addToCart(Integer categoryId, Integer count, Integer userId){
		CartExample example = new CartExample();
		example.createCriteria().andUserIdEqualTo(userId);
		List<Cart> carts = cartMapper.selectByExample(example);
		if (CollectionUtils.isEmpty(carts)) {
			Cart cart = new Cart();
			cart.setUserId(userId);
			cartMapper.createCart(cart);
			carts = cartMapper.selectByExample(example);
		}
		InventoryExample example2 = new InventoryExample();
		example2.createCriteria().andInventoryIdEqualTo(carts.get(0).getInventoryId()).andCategoryIdEqualTo(categoryId);
		List<Inventory> inventories = inventoryMapper.selectByExample(example2);
		if (CollectionUtils.isEmpty(inventories)) {
			Inventory inventory = new Inventory();
			inventory.setCategoryId(categoryId);
			inventory.setQuantity(count);
			inventory.setInventoryId(carts.get(0).getInventoryId());
			inventoryMapper.insertSelective(inventory);
		}
		return getCartItem(userId);
	}
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.ICartService#getCartItem(java.lang.Integer)
	 */
	@Override
	public List<CartBean> getCartItem(Integer userId){
		List<CartBean> cartBeans = categoryMapper.getCartItem(userId);
		if (CollectionUtils.isEmpty(cartBeans)) {
			return new ArrayList<CartBean>();
		}
		for (CartBean cartBean : cartBeans) {
			Integer imageId = entityMapper.getImageId(cartBean.getId()).get(0);
			cartBean.setImageId(imageId);
		}
		return cartBeans;
	}
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.ICartService#getItemSummary(java.lang.Integer)
	 */
	@Override
	public Integer getItemSummary(Integer userId){
		if (StringUtils.isEmpty(userId)) {
			return 0;
		}
		int count = cartMapper.getItemSummary(userId);
		return count;
	}
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.ICartService#removeItem(java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public boolean removeItem(Integer itemId, Integer userId){
		int count = cartMapper.deleteCartItem(itemId, userId);
		if (count <= 0) {
			return false;
		}
		return true;
	}
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.ICartService#removeAll(java.lang.Integer)
	 */
	@Override
	public boolean removeAll(Integer userId){
		int count = cartMapper.deleteAllItems(userId);
		if (count <= 0) {
			return false;
		}
		return true;
	}
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.ICartService#getCart(java.lang.Integer)
	 */
	@Override
	public boolean getCart(Integer userId){
		CartExample example = new CartExample();
		example.createCriteria().andUserIdEqualTo(userId);
		List<Cart> carts = cartMapper.selectByExample(example);
		if (CollectionUtils.isEmpty(carts)) {
			return false;
		}
		return true;
	}
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.ICartService#getInventoryId(java.lang.Integer)
	 */
	@Override
	public String getInventoryId(Integer userId){
		CartExample example = new CartExample();
		example.createCriteria().andUserIdEqualTo(userId);
		List<Cart> carts = cartMapper.selectByExample(example);
		if (CollectionUtils.isEmpty(carts)) {
			throw new RemoteSensingException(Constants.ERROR_NULL_CART);
		}
		return carts.get(0).getInventoryId();
	}
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.ICartService#removeCart(java.lang.Integer)
	 */
	@Override
	public void removeCart(Integer userId){
		CartExample example = new CartExample();
		example.createCriteria().andUserIdEqualTo(userId);
		int i = cartMapper.deleteByExample(example);
		if (i <= 0) {
			throw new RemoteSensingException(Constants.ERROR_NULL_CART);
		}
		
	}
}
